package com.jumeng.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jumeng.entity.CourseEntity;
import com.jumeng.entity.CourseUserReleationEntity;
import com.jumeng.mapper.CourseMapper;
import com.jumeng.mapper.CourseUserRelationMapper;
import com.jumeng.query.CoursesQuery;
import com.jumeng.resp.CoursesRes;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jumeng.server.CourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, CourseEntity> implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private CourseUserRelationMapper courseUserRelationMapper;

    @Override
    public Page<CoursesRes> page(CoursesQuery coursesQuery) {
        LambdaQueryWrapper<CourseEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(!StringUtils.isEmpty(coursesQuery.getSearchName()), CourseEntity::getCourseName, coursesQuery.getSearchName());
        wrapper.eq(coursesQuery.getCourseEnum() != null, CourseEntity::getCourseEnum, coursesQuery.getCourseEnum());
        Page<CourseEntity> coursesResPage = new Page<>(coursesQuery.getCurrentPage(), coursesQuery.getPageSize());
        Page<CourseEntity> courseEntityPage = courseMapper.selectPage(coursesResPage, wrapper);
        List<CoursesRes> coursesResList = courseMapper.selectPage(coursesResPage, wrapper).getRecords()
                .stream()
                .map(CoursesRes::new)
                .collect(Collectors.toList());
        Page<CoursesRes> res = new Page<>();
        res.setTotal(courseEntityPage.getTotal());
        res.setRecords(coursesResList);
        return res;
    }

    @Override
    public CoursesRes details(String accountStationNo, String courseCode) {
        LambdaQueryWrapper<CourseEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CourseEntity::getCode, courseCode);
        CourseEntity courseEntity = courseMapper.selectOne(wrapper);
        CoursesRes coursesRes = new CoursesRes(courseEntity);
        // 设置账户编码
        coursesRes.setAccountStationNo(accountStationNo);
        LambdaQueryWrapper<CourseUserReleationEntity> relationWrapper = Wrappers.lambdaQuery();
        relationWrapper.eq(CourseUserReleationEntity::getCourseCode, courseCode);
        relationWrapper.eq(CourseUserReleationEntity::getAccountStationNo, accountStationNo);
        CourseUserReleationEntity courseUserReleationEntity = courseUserRelationMapper.selectOne(relationWrapper);
        // todo 此处暂时所有都是可以看
        coursesRes.setCanShow(true);
//        if (courseUserReleationEntity != null) {
//            coursesRes.setCanShow(true);
//        }
        return coursesRes;
    }
}
